/* Author: alex

 */
Namespace("sketch.framework");

(function() {
    // usings
    var fwk = sketch.framework;
    var dec = sketch.decorators;

    fwk.SelectComposite = klass(fwk.UIElement, {
        __type__:"framework.SelectComposite",
        _constructor : function(ownerView) {
            this.elements = [];
            this.orderedElements = new fwk.OrderedCollection();
            this._selected = false;
            this._isSelfDestroying = false;
            this._canDraw = false;

            this.selected = function(value) {
                if (arguments.length === 1) {
                    if (value === this._selected) {
                        return;
                    }

                    this._selected = value;

                    this.each(function(element) {
                        if (value) {
                            element.addDecorator(new dec.ActiveFrame());
                        }
                        else {
                            element.removeDecoratorByType(dec.ActiveFrame);
                        }
                    });
                }

                return this._selected;
            }
        },
        add : function(element) {
            this.elements.push(element);
            this.orderedElements.add(element);
        },
        elementAt: function(index){
            return this.elements[index];
        },
        has: function(element){
            for (var i = 0, j = this.elements.length; i < j; ++i) {
                if (this.elements[i] === element) {
                    return true;
                }
            }
            return false;
        },
        clear : function() {
            this._canDraw = false;
            this.elements = [];
            this.orderedElements.clear();
        },
        hitTest : function(x, y) {
            var count = this.count();
            if (count <= 1){
                return false;
            }

            for (var i = count - 1; i >= 0; i--) {
                var el = this.elements[i];
                var pos = el.parent().global2local({x:x, y:y});
                if (el.hitTest(pos.x, pos.y)) {
                    return true;
                }
            }
            return false;
        },
        count : function() {
            return this.elements.length;
        },
        addDecorator:function() {
        },
        canSelect: function() {
            return false;
        },
        canAccept: function(element){
            return false;
        },
        each: function(callback) {
            each(this.elements, callback);
        },
        eachOrdered: function(callback){
            this.orderedElements.each(callback);
        },
        drawSelf: function(context) {
            if (this._canDraw) {
                context.save();
                context.fillStyle = 'red';
                context.fillRect(this.left(), this.top(), this.width(), this.height());
                context.restore();
                this.eachOrdered(function(i, element) {
                    context.save();
                    var p = element.position();
                    var gp = element.parent().local2global(p);
                    context.translate(gp.x - p.x, gp.y - p.y);
                    element.draw(context);
                    context.restore();
                });
            }
        },
        min: function(accessor) {
            var result = null;

            this.each(function(element) {
                var value = accessor(element);
                if (result === null || value < result) {
                    result = value;
                }
            });

            return result;
        },
        max: function(accessor) {
            var result = null;

            this.each(function(element) {
                var value = accessor(element);
                if (result === null || value > result) {
                    result = value;
                }
            });

            return result;
        },
        left: function(value) {
            if (arguments.length === 0) {
                return this.min(function(element) {
                    var gr = element.getBoundaryRectGlobal();
                    return gr.x;
                });
            }

            var diff = value - this.left();

            this.each(function(element) {
                element.left(element.left() + diff);
            });
        },
        top: function(value) {
            if (arguments.length === 0) {
                return this.min(function(element) {
                    var gr = element.getBoundaryRectGlobal();
                    return gr.y;
                });
            }

            var diff = value - this.top();

            this.each(function(element) {
                element.top(element.top() + diff);
            });
        },
        width: function(value) {
            return this.max(function(element) {
                var gr = element.getBoundaryRectGlobal();
                return gr.x + gr.width;
            }) - this.left();
        },
        height: function(value) {
            return this.max(function(element) {
                var gr = element.getBoundaryRectGlobal();
                return gr.y + gr.height;
            }) - this.top();
        },
        getVisualActions: function() {
            var bag = new fwk.ActionBag();
            var that = this;

            bag.add("Align", function() {
                return new fwk.commands.Align("left", "Align Left", that.elements);
            }, "/img/ui/align-left.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("right", "Align Right", that.elements);
            }, "/img/ui/align-right.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("top", "Align Top", that.elements);
            }, "/img/ui/align-top.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("bottom", "Align Bottom", that.elements);
            }, "/img/ui/align-bottom.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("center", "Align Center", that.elements);
            }, "/img/ui/align-center.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("middle", "Align Middle", that.elements);
            }, "/img/ui/align-middle.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("distributeHorizontally", "Distribute Horizontally", that.elements);
            }, "/img/ui/distribute-horizontally.png");

            bag.add("Align", function() {
                return new fwk.commands.Align("distributeVertically", "Distribute Vertically", that.elements);
            }, "/img/ui/distribute-vertically.png");

            return bag;
        },
        clone: function() {
            var that = this;
            var clone = this.shallowCopy();

            this.each(function(element) {
                clone.add(element.clone());
            });

            clone.properties.parent.bind(function(event) {
                if (event.newValue instanceof fwk.NullContainer) {
                    if (!clone._isSelfDestroying) {
                        clone.each(function(element) {
                            clone._parentOfChildren.remove(element);
                        });
                    }
                } else {
                    var newParent = event.newValue;

                    newParent.lockArrange();
                    clone.each(function(element) {
                        newParent.add(element);
                    });
                    clone._isSelfDestroying = true;
                    newParent.remove(clone);
                    clone._isSelfDestroying = false;

                    clone._parentOfChildren = newParent;
                    newParent.unlockArrange();
                }
            });

            return clone;
        },
        isDescendantOrSame:function(){
            return true;
        },
        parent: function(parent){
            if (arguments.length === 0){
                if (this.elements.length !== 0) {
                    return this.elements[0].parent();
                }
            }

            return this.SuperKlass.parent.apply(this, arguments);
        },
        startDrag: function(){
            this._canDraw = true;
        }
    });
})();
